package com.youka.bussiness.common.bean;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youka.bussiness.common.base.IExceptionEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.core.Conventions;

import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author cai
 */
@ApiModel(value = "公共出参")
public class RestResp extends LinkedHashMap<String, Object> {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "返回码")
    private static final String CODE = "code";

    @ApiModelProperty("返回信息")
    private static final String MSG = "message";

    @ApiModelProperty(value = "返回数据")
    private static final String DATA = "data";

    private static final String COUNT = "count";

    private LinkedHashMap<String, Object> data;

    private int sameKeyCount = 0;

    private RestResp() {
    }

    public static RestResp error() {
        return error("1", "未知异常，请稍后重试！");
    }

    public static RestResp error(String msg) {
        return error("1", msg);
    }

    public static RestResp error(String code, String msg) {
        RestResp resp = new RestResp();
        resp.code(code);
        resp.msg(msg);
        return resp;
    }

    public static RestResp error(IExceptionEnum ee) {
        RestResp resp = new RestResp();
        resp.code(String.valueOf(ee.getCode()));
        resp.msg(ee.getMessage());
        return resp;
    }

    public static RestResp success(String msg) {
        RestResp resp = new RestResp();
        resp.code("0");
        resp.msg(msg);
        return resp;
    }

    public static RestResp success() {
        return RestResp.success("success");
    }

    public static <T> RestResp success(T t) {
        return success().append(t);
    }

    private void code(String code) {
        super.put(CODE, code);
    }

    private void msg(String msg) {
        super.put(MSG, msg);
    }

    public RestResp append(String key, Object value) {
        // 初始化响应数据体
        if (data == null) {
            data = new LinkedHashMap<>();
            super.put(DATA, data);
        }
        // 同名数据处理
        if (data.containsKey(key)) {
            key += ++sameKeyCount;
            append(key, value);
        }
        sameKeyCount = 0;
        data.put(key, value);
        return this;
    }

    public RestResp append(Object value) {
        if (value == null) {
            return this;
        }
        append(Conventions.getVariableName(value), value);
        return this;
    }

    public RestResp setData(Object obj) {
        super.put(DATA, obj);
        return this;
    }

    public RestResp toAdminPage(IPage page) {
        super.put(DATA, page.getRecords());
        super.put(COUNT, page.getTotal());
        super.put("page", page.getCurrent());
        super.put("limit", page.getSize());
        return this;
    }

    public RestResp toAdminTree(List list) {
        super.put(DATA, list);
        return this;
    }

}
